<%--                                                                       --%>
<%-- Copyright (c) 2001-2004 BroadVision, Inc.  All rights reserved.       --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>

<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>

<% String ctxPath = request.getContextPath(); %>

<script language="JavaScript">

  function doCustomSubmit(theForm) {
    var customPickerAction = '<c:out value="${requestScope.customPickerAction}"/>';
    if (customPickerAction != null && customPickerAction.length > 0) {
      theForm.action = '<c:url value="${requestScope.customPickerAction}"/>';
    }
    theForm.submit();
  }
  
  function closeWindow() {
    self.close();
  }
  
  function doContextGo() {
    var theForm = document.mtFilePickerForm;
    var pagerOffsetField = findField(theForm, 'pager.offset');
    if (pagerOffsetField != null) {
      pagerOffsetField.value = 0;
    }
    var field = findField(theForm, 'selectedContext');
    if (field != null) {
      theForm.selectedDirectory.value = '';
      doCustomSubmit(theForm);
    }
  }
  
  function doSelectCurrentFolderAction(theIndex) {
    var theForm = document.mtFilePickerForm;
    var pagerOffsetField = findField(theForm, 'pager.offset');
    if (pagerOffsetField != null) {
      pagerOffsetField.value = 0;
    }
    var fieldName = 'cdir[' + theIndex + '].path';
    var field = findField(theForm, fieldName);
    if (field != null) {
      theForm.selectedDirectory.value = field.value;
      doCustomSubmit(theForm);
    }
  }

  function doSelectFolderAction(theIndex) {
    var theForm = document.mtFilePickerForm;
    var pagerOffsetField = findField(theForm, 'pager.offset');
    if (pagerOffsetField != null) {
      pagerOffsetField.value = 0;
    }
    var fieldName = 'dir[' + theIndex + '].path';
    var field = findField(theForm, fieldName);
    if (field != null) {
      theForm.selectedDirectory.value = field.value;
      doCustomSubmit(theForm);
    }
  }
  
  function doSelectFileAction(theIndex) {
    var theForm = document.mtFilePickerForm;
    var fieldName = 'file[' + theIndex + '].path';
    var field = findField(theForm, fieldName);
    if (field != null) {
      callParentWindow(field.value);
    }
  }
  
  function findField(theForm, fieldName) {
    var elements = theForm.elements;
    var numElements = elements.length;
    for (var elementIndex = 0; elementIndex < numElements; elementIndex++) {
      var elementName = elements[elementIndex].name;
      if (elementName == fieldName) {
        return elements[elementIndex];
      }
    }
    return null;
  }

  function callParentWindow(relativePath) {
    if (self.opener != null) {
      self.opener.<c:out value="${mtFilePickerForm.map.callback}"/>(relativePath);
    }
    self.close();
  }

  function doValidation(theForm) {
    var found = false;
    if (theForm.selectedFile.length == null) {
      if (theForm.selectedFile.checked == true) {
        found = true;
        callParentWindow(theForm.selectedFile.value);
      }
    }
    else {
      for (var i = 0; i < theForm.selectedFile.length; i++) {
        if (theForm.selectedFile[i].checked == true) {
          found = true;
          callParentWindow(theForm.selectedFile[i].value);
          break;
        }
      }
    }
    if (found == false) {
      alert('<bean:message key="mt.file.picker.selectOneFile" bundle="mt.messages"/>');
    } 
  }

</script>

<c:set var="dirImg">
<img src="<%= ctxPath %>/mc/images/icons/icon_folder.gif" width="18" height="16" border="0" alt="<bean:message key="image.alt.folder" bundle="mt.messages"/>" title="<bean:message key="image.alt.folder" bundle="mt.messages"/>">
</c:set>

<c:set var="fileImg">
<img src="<%= ctxPath %>/mc/images/icons/icon_file.gif" width="18" height="16" border="0" alt="<bean:message key="image.alt.file" bundle="mt.messages"/>" title="<bean:message key="image.alt.file" bundle="mt.messages"/>">
</c:set>

<c:set var="okImg">
<img src="<%= ctxPath %>/mc/images/action/icon_ok.gif" width="15" height="15" border="0" class="actionIcon" alt="<bean:message key="image.alt.select" bundle="mt.messages"/>" title="<bean:message key="image.alt.select" bundle="mt.messages"/>">
</c:set>

<html-el:form action="/mt/filePicker.do" onsubmit="doValidation(mtFilePickerForm); return false;">
<html-el:hidden property="fileFilter"/>
<html-el:hidden property="folderFilter"/>
<html-el:hidden property="callback"/>
<html-el:hidden property="selectedDirectory"/>
<html-el:hidden property="headerKey"/>
<html-el:hidden property="headerBundle"/>
<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr>
    <td valign="top" width="100%">
      <div class="pageTitle">
        <c:out value="${mtFilePickerView.map.header}"/>
      </div>
      <div class="generalWrapRight">
        <html-el:button property="closeWinBtn" styleClass="inputButton" onclick="javascript:closeWindow()">
          <bean:message key="button.close" bundle="mt.messages"/>
        </html-el:button>
      </div>
      <div class="errorMessage"><html-el:errors bundle="mt.messages"/></div>
<c:choose>
  <c:when test="${not empty mtFilePickerView.map.contextList}">
    <bean:size id="contextListSize" name="mtFilePickerView" property="contextList"/>
    <c:choose>
      <c:when test="${contextListSize > 1}">
      <div class="pageButtonWrap" align="left">
        <b><bean:message key="mt.file.picker.appContext" bundle="mt.messages"/>:</b>&nbsp;
        <html-el:select property="selectedContext" size="1">
          <html-el:optionsCollection name="mtFilePickerView" property="contextList"/>
        </html-el:select>
        <html-el:button property="selectApp" styleClass="inputButton" onclick="doContextGo();">
          <bean:message key="button.goto" bundle="mt.messages"/>
        </html-el:button>
      </div>
      </c:when>
      <c:otherwise>
      <html-el:hidden property="selectedContext"/>
      </c:otherwise>
    </c:choose>
  </c:when>
  <c:otherwise>
      <html-el:hidden property="selectedContext"/>
  </c:otherwise>
</c:choose>
<c:if test="${not empty mtFilePickerView.map.currentDirList}">
      <div class="breadcrumb">
  <c:forEach var="cdir" items="${mtFilePickerView.map.currentDirList}" varStatus="status">
        <html-el:hidden name="cdir" property="path" indexed='true'/>
    <c:if test="${status.first && status.last}">
        <bean:message key="mt.file.picker.root" bundle="mt.messages"/>
      <c:set var="currentDirectory">
        <bean:message key="mt.file.picker.root" bundle="mt.messages"/>
      </c:set>
    </c:if>
    <c:if test="${status.first && not status.last}">
        <a href='#' onclick="doSelectCurrentFolderAction('<c:out value="${status.index}"/>');">
          <bean:message key="mt.file.picker.root" bundle="mt.messages"/></a>
        <bean:message key="mt.file.picker.path.delimiter" bundle="mt.messages"/>
    </c:if>
    <c:if test="${not status.first && not status.last}">
        <a href='#' onclick="doSelectCurrentFolderAction('<c:out value="${status.index}"/>');">
          <c:out value="${cdir.map.name}"/></a>
        <bean:message key="mt.file.picker.path.delimiter" bundle="mt.messages"/>
    </c:if>
    <c:if test="${not status.first && status.last}">
        <c:out value="${cdir.map.name}"/>
      <c:set var="currentDirectory">
        <c:out value="${cdir.map.name}"/>
      </c:set>
    </c:if>
  </c:forEach>
      </div>
</c:if>
      <div class="pageSubtitle">
        <span class="left">
<c:if test="${not empty mtFilePickerView.map.subDirList}">
          <bean-el:message key="mt.file.picker.foldersWithin" arg0="${currentDirectory}" bundle="mt.messages"/>
</c:if>
        </span>
        <span class="right">&nbsp;</span>
      </div>
      <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="folders table" width="100%">
        <tr class="listHeader">
          <th id="Name" align="left">
            <bean:message key="mt.header.name.folder" bundle="mt.messages"/>
          </th>
        </tr>
<c:choose>
  <c:when test="${not empty mtFilePickerView.map.subDirList}">
    <c:forEach var="dir" items="${mtFilePickerView.map.subDirList}" varStatus="status">
      <c:choose>
        <c:when test="${(status.count + 1) % 2 == 0}">
	      <tr class="listEven">
        </c:when> 
  	    <c:otherwise>
	      <tr class="listOdd">
	      </c:otherwise>
      </c:choose>
          <td headers="Name">
            <html-el:hidden name="dir" property="path" indexed='true'/>
            <c:out value='${dirImg}' escapeXml="false"/>
            <a href='#' onclick="doSelectFolderAction('<c:out value="${status.index}"/>');">
              <c:out value="${dir.map.name}"/></a>
          </td>
        </tr>
    </c:forEach>
  </c:when>
  <c:otherwise>
      <tr class="listOdd">
        <td align="center" colspan="1">
          <bean:message key="mt.file.picker.folders.noFound" bundle="mt.messages"/>
        </td>
      </tr>
  </c:otherwise>
</c:choose>
      </table>
      <div class="pageSubtitle">
        <span class="left">
<c:if test="${not empty mtFilePickerView.map.fileList}">
          <bean-el:message key="mt.file.picker.filesWithin" arg0="${currentDirectory}" bundle="mt.messages"/>
</c:if>
        </span>
        <span class="right">&nbsp;</span>
      </div>
      <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="files table" width="100%">
        <tr class="listHeader">
          <th id="Radiobuttons">&nbsp;</th>
          <th id="Name" align="left">
            <bean:message key="mt.header.name.file" bundle="mt.messages"/>
          </th>
          <th id="Actions" align="left">
            <bean:message key="mt.header.actions" bundle="mt.messages"/>
          </th>
        </tr>
<c:choose>
  <c:when test="${not empty mtFilePickerView.map.fileList}">
    <c:forEach var="file" items="${mtFilePickerView.map.fileList}" varStatus="status">
      <c:choose>
        <c:when test="${(status.count + 1) % 2 == 0}">
	      <tr class="listEven">
        </c:when> 
  	    <c:otherwise>
	      <tr class="listOdd">
	      </c:otherwise>
      </c:choose>
          <td headers="Radiobuttons">
            <html-el:radio property="selectedFile" value="${file.map.path}"/>
          </td>
          <td headers="Name">
            <c:out value='${fileImg}' escapeXml="false"/>
            <c:out value="${file.map.name}"/>
          </td>
          <td headers="Actions">
            <html-el:hidden name="file" property="path" indexed='true'/>
            <a href='#' onclick="doSelectFileAction('<c:out value="${status.index}"/>');">
              <c:out value='${okImg}' escapeXml="false"/></a>
          </td>
        </tr>
    </c:forEach>
  </c:when>
  <c:otherwise>
      <tr class="listOdd">
        <td align="center" colspan="3">
          <html-el:hidden property="selectedFile"/>
          <bean:message key="mt.file.picker.files.noFound" bundle="mt.messages"/>
        </td>
      </tr>
  </c:otherwise>
</c:choose>
      </table>
<c:if test="${not empty mtFilePickerView.map.contextList}">
<%@ include file="/mc/common/pager.jsp" %>
</c:if>
      <div class="pageButtonWrap" align="right">
        <input type="button" name="cancelData" class="pageButton" onClick="javascript:closeWindow()" 
          value='<bean:message key="button.cancel" bundle="mt.messages"/>'>
<c:if test="${not empty mtFilePickerView.map.fileList}">
        <input type="submit" name="selectFile" class="pageButton" 
          value='<bean:message key="button.done" bundle="mt.messages"/>'>
</c:if>
      </div>
    </td>
  </tr>
</table>
</html-el:form>
